package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.aspectj.bridge.Message;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 李希
 * @date 2019/11/18
 * Time: 21:20
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;
//    PageResult总记录数和数据
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return addressService.findPage(queryPageBean);
    }
    @RequestMapping("/add.do")
    public Result add(@RequestBody Address address){
        try {
            addressService.add(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }
    @RequestMapping("/delete.do")
    public Result delete(Integer id){
        try {
            addressService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }

    }

    @RequestMapping("/downAddressExcel.do")
    public Result downAddressExcel(HttpServletResponse response) throws IOException {
        List<Address> all = addressService.findAll();
        System.out.println(all.get(0));
        XSSFWorkbook sheets = new XSSFWorkbook();
        XSSFSheet sheet = sheets.createSheet();
        XSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("id");
        row1.createCell(1).setCellValue("地址");
        row1.createCell(2).setCellValue("经度");
        row1.createCell(3).setCellValue("纬度");
        for(int i=0;i<all.size();i++){
            XSSFRow row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(all.get(i).getId());
            row.createCell(1).setCellValue(all.get(i).getAddress());
            row.createCell(2).setCellValue(all.get(i).getLon());
            row.createCell(3).setCellValue(all.get(i).getLat());
        }

        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition","attachment;filename=address.xlsx");
        sheets.write(os);
        os.flush();
        os.close();
        sheets.close();
        System.out.println(sheet.getRow(1).getCell(1));
        return new Result(true, MessageConstant.DERIVE_ADDRESS_SUCCESS);
    }
}